import 'package:flutter/cupertino.dart';
import 'package:flutter_give/utils/LogUtils.dart';
import 'package:flutter_give/utils/constants.dart';
import 'package:flutter_give/repository/api.dart';
import 'package:flutter_give/utils/sp_utils.dart';
import 'package:url_launcher/url_launcher.dart';

/// api方法类
class PersonViewModel with ChangeNotifier {
  String? username;
  String? eqdatalist;
  String? usernamephone;
  bool shouldLogin = false;

  Future initData() async {
    usernamephone = await SpUtils.getString(Constants.SP_USER_PHONE);
    eqdatalist = await SpUtils.getString(Constants.BUT_EQLIST_PRICE);
    SpUtils.getString(Constants.SP_USER_NAME).then(
      (value) async => {
        LogUtils.w("数据${eqdatalist}"),

        // print("姓名======================${value}"),
        if (value == null || value == "")
          {username = "未登录", shouldLogin = true}
        else
          {
            username = value,
            // print("姓名======================${username}"),
            shouldLogin = false
          },

        notifyListeners()

      },
    );
  }

  // 退出登录
  // 设置回调 ValueChanged<bool>
  Future logout(ValueChanged<bool> callback) async {
    // bool? success = await Api.instance.logout();
    // if (success == true) {
    //   await SpUtils.removeAll();
    //   callback.call(true);
    // } else {
    //   callback.call(false);
    // }
  }

  ///跳转到外部浏览器打开
  Future jumpToOutLink(String? url) async {
    final uri = Uri.parse(url ?? "");
    if (await canLaunchUrl(uri)) {
      return launchUrl(uri);
    }
    return null;
  }
}
